import {
  InsertData, DeleteData, UpdateData, SearchData,
  SelectDatabase,
} from '@/utils/db'
/**
 * 添加用户
 * @param request
 * @param response
 */
const insertUser = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  let { id } = queryParams
  const _db = SelectDatabase('bug-manager', 'organization', 'UserList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    const updateDataCallback = (resJSON: any) => {
      UpdateData(_db, { id }, queryParams, resJSON, sendData)
    }
    SearchData(_db, { id }, resJSON, updateDataCallback)
  } else {
    InsertData(_db, queryParams, resJSON, sendData)
  }
}
/**
 * 获取用户列表
 * @param request
 * @param response
 */
const getUserList = async (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'organization', 'UserList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  // 判断字典是否存在
  SearchData(_db, {}, resJSON, sendData)
}

export default [
  {
    path: '/bugManager/organization/insertUser',
    func: insertUser,
  },
  {
    path: '/bugManager/organization/getUserList',
    func: getUserList,
  },
]